﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DALEntities;
using MapEntities;
using ServerPlatform.Web;
using ServerPlatform.Exceptions;

namespace WebApp.Controls
{
    public partial class BaseBindingRuleEditor : BaseDataEditor<BaseBindingRule>
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        public BaseBindingRule ItemRule { get; set; }

        protected void ctlSaveProvider_Click(object sender, EventArgs e)
        {
            string selectProvider = ctlDataSourceList.SelectedValue;
            SchemaRequest request = new SchemaRequest()
            {
                ConnectionString = ctlConnectionString.Text,
                ID = Guid.NewGuid(),
                ProviderName = selectProvider
            };

            var result = DataHelper.GetSchema(request);
            ctlViewPanel.Visible = true;

            ctlViewList.DataSource = result;
            ctlViewList.DataBind();
            ViewState["currentSchema"] = result;            
            ctlViewList.SelectedIndex = ctlViewList.Items.IndexOf(ctlViewList.Items.FindByText(Current.Request.ViewName));
            ctlViewList_SelectedIndexChanged(ctlViewList, e);
            ctlKeyFieldList.SelectedIndex = ctlKeyFieldList.Items.IndexOf(ctlKeyFieldList.Items.FindByText(Current.KeyField));
        }


        protected override void ShowEntity(BaseBindingRule entity)
        {
            ctlDataSourceList.DataSource = DataHelper.GetProviders();
            ctlDataSourceList.DataBind();
            ctlConnectionString.Text = string.IsNullOrEmpty(entity.Request.ConnectionString) ? string.Empty : entity.Request.ConnectionString;
            ctlFiltet.Text = entity.Request.ExtraData == null ? "" : entity.Request.ExtraData.ToString();
            if (entity.KeyField != null && entity.Request.ViewName != null)
            {                
                ctlDataSourceList.SelectedIndex = ctlDataSourceList.Items.IndexOf(ctlDataSourceList.Items.FindByValue(entity.Request.ProviderName));
                ctlViewList.SelectedIndex = ctlViewList.Items.IndexOf(ctlViewList.Items.FindByValue(entity.Request.ViewName));
                ctlKeyFieldList.SelectedIndex = ctlKeyFieldList.Items.IndexOf(ctlKeyFieldList.Items.FindByValue(entity.KeyField));
            }
            else
            {                
                ctlDataSourceList.SelectedIndex = 0;
                ctlKeyFieldList.Items.Clear();
                ctlViewList.Items.Clear();
                
            }

        }

        public override BaseBindingRule GetEntity()
        {
            BaseBindingRule entity = Current;
            //if (string.IsNullOrEmpty(ctlConnectionString.Text.Trim()))
            //{
            //    BusinessException ee = new BusinessException("Connection String cannot be empty");
            //    throw ee;
            //}
            entity.Request.ConnectionString = ctlConnectionString.Text.Trim();                        
            entity.Request.ProviderName = ctlDataSourceList.SelectedValue;
            if(!string.IsNullOrEmpty(ctlViewList.SelectedValue.Trim()) && ctlViewList.SelectedIndex != -1) entity.Request.ViewName = ctlViewList.SelectedValue;
            if (!string.IsNullOrEmpty(ctlKeyFieldList.SelectedValue.Trim()) && ctlKeyFieldList.SelectedIndex !=-1) entity.KeyField = ctlKeyFieldList.SelectedValue;
            entity.Request.ExtraData = ctlFiltet.Text;
            return entity;
        }

        protected void ctlViewList_SelectedIndexChanged(object sender, EventArgs e)
        {
            DALEntities.ViewSchema[] list = (DALEntities.ViewSchema[])ViewState["currentSchema"];
            ViewSchema selected = list.Single(p => p.Name == ctlViewList.SelectedValue);
            ctlKeyFieldList.DataSource = selected.Columns;
            ctlKeyFieldList.DataBind();
        }


    }
}